package com.bwie.gddproject.index.view.fragment;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.bwie.gddproject.R;
import com.bwie.gddproject.index.adapter.HomeAdapter;
import com.bwie.gddproject.index.model.bean.IndexBean;
import com.bwie.gddproject.index.precenter.IIndexPrecenter;
import com.bwie.gddproject.index.precenter.IndexPrecenter;
import com.bwie.gddproject.index.view.BannerActivity;
import com.bwie.gddproject.index.view.XiaoXiActivity;
import com.bwie.gddproject.index.view.wight.LooperTextView;
import com.bwie.gddproject.login.view.LoginActivity;
import com.bwie.gddproject.search.view.SearchActivity;
import com.bwie.gddproject.utils.GlideImageLoader;
import com.bwie.gddproject.xiangqing.view.XiangQingActivity;
import com.facebook.drawee.view.SimpleDraweeView;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.youth.banner.Banner;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by 我走路带风 on 2017/11/9.
 */

public class IndexFragment extends Fragment implements IIndexFragment, View.OnClickListener {

    private RecyclerView mRv;
    private SmartRefreshLayout mSmartlayout;
    private LinearLayout mSaoyisao;
    private RelativeLayout mSousuo;
    private LinearLayout mXiaoxi;
    private Banner mBanner;
    private ViewPager mVp;
    private LinearLayout mPoints;
    private ImageView mB1;
    private ImageView mB2;
    private ArrayList<Fragment> fragments;
    private GridFragment gridFragment;
    private Grid2Fragment gridFragment2;
    private List<IndexBean.TuijianBean.ListBean> tuiJianList;
    private HomeAdapter homeAdapter;
    private View view;
    /**
     * 笔记本电脑
     */
    private EditText mEt;
    private SimpleDraweeView mXimg;
    private LooperTextView mLtv;
    private LinearLayout mHomepageNoticeLl;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = View.inflate(getActivity(), R.layout.index_fragment, null);
        initView(view);
        IIndexPrecenter iIndexPrecenter = new IndexPrecenter(this);
        iIndexPrecenter.getDatas();
        freshAndLoadMore();
        return view;
    }

    @Override
    public void showDates(IndexBean indexBean) {
        final List<IndexBean.DataBean> data = indexBean.getData();
        final List<String> banner = new ArrayList<>();
        for (int i = 0; i < data.size(); i++) {
            String icon = data.get(i).getIcon();
            banner.add(icon);
        }

        mBanner.setImages(banner);
        mBanner.isAutoPlay(true);
        mBanner.setDelayTime(3000);
        mBanner.setImageLoader(new GlideImageLoader());
        mBanner.start();
        mBanner.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getActivity(), BannerActivity.class);
                intent.putExtra("url", data.get(0).getUrl());
                startActivity(intent);
            }
        });


        tuiJianList = indexBean.getTuijian().getList();
        homeAdapter = new HomeAdapter(tuiJianList, getContext());

        mRv.setAdapter(homeAdapter);
        mRv.setLayoutManager(new GridLayoutManager(getContext(), 2));

        homeAdapter.setOnItemClickListener(new HomeAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                String pid = tuiJianList.get(position).getPid();
                Intent intent = new Intent(getActivity(), XiangQingActivity.class);
                intent.putExtra("pid", pid);
                startActivity(intent);
            }
        });

        //初始化圆点
        mB1.setImageResource(R.drawable.dot_select);
        fragments = new ArrayList<>();
        gridFragment = new GridFragment();
        gridFragment2 = new Grid2Fragment();
        fragments.add(gridFragment);
        fragments.add(gridFragment2);
        mVp.setAdapter(new FragmentPagerAdapter(getFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return fragments.get(position);
            }

            @Override
            public int getCount() {
                return fragments.size();
            }
        });

        mVp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                switch (position) {
                    case 0:
                        mB1.setImageResource(R.drawable.dot_select);
                        mB2.setImageResource(R.drawable.dot_normal);
                        break;
                    case 1:
                        mB2.setImageResource(R.drawable.dot_select);
                        mB1.setImageResource(R.drawable.dot_normal);
                        break;
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        ArrayList<String> tips = new ArrayList<>();
        tips.add("老外的'非人类'玩具");
        tips.add("衣服大一号,人就瘦一圈?");
        tips.add("闪瞎:全球最贵五辆摩托车");
        tips.add("天使同款,维密的同款居然是它!");
        tips.add("SONY居然要消灭充电宝");
        tips.add("三星Note8居然在中国买不动?");
        tips.add("男人也需要'防狼'");
        mLtv.setTipList(tips);
    }

    @Override
    public void toast(String str) {
        Toast.makeText(getContext(), str, Toast.LENGTH_SHORT).show();
    }

    private void initView(View view) {
        mRv = (RecyclerView) view.findViewById(R.id.rv);
        mSmartlayout = (SmartRefreshLayout) view.findViewById(R.id.smartlayout);
        mSaoyisao = (LinearLayout) view.findViewById(R.id.saoyisao);
        mSousuo = (RelativeLayout) view.findViewById(R.id.sousuo);
        mXiaoxi = (LinearLayout) view.findViewById(R.id.xiaoxi);
        mBanner = (Banner) view.findViewById(R.id.banner);
        mVp = (ViewPager) view.findViewById(R.id.vp);
        mSaoyisao.setOnClickListener(this);
        mSousuo.setOnClickListener(this);
        mXiaoxi.setOnClickListener(this);
        mBanner.setOnClickListener(this);
        mVp.setOnClickListener(this);
        mPoints = (LinearLayout) view.findViewById(R.id.points);
        mRv.setOnClickListener(this);
        mSmartlayout.setOnClickListener(this);
        mB1 = (ImageView) view.findViewById(R.id.b1);
        mB2 = (ImageView) view.findViewById(R.id.b2);
        mPoints.setOnClickListener(this);
        mB1.setOnClickListener(this);
        mB2.setOnClickListener(this);
        mEt = (EditText) view.findViewById(R.id.et);
        mEt.setOnClickListener(this);
        mXimg = (SimpleDraweeView) view.findViewById(R.id.ximg);
        mLtv = (LooperTextView) view.findViewById(R.id.ltv);
        mHomepageNoticeLl = (LinearLayout) view.findViewById(R.id.homepage_notice_ll);
    }

    //刷新加载更多
    private void freshAndLoadMore() {
        mSmartlayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                IIndexPrecenter iIndexPrecenter = new IndexPrecenter(IndexFragment.this);
                iIndexPrecenter.getDatas();
                refreshlayout.finishRefresh();//传入false表示刷新失败
            }
        });
        mSmartlayout.setOnLoadmoreListener(new OnLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                tuiJianList.addAll(tuiJianList);
                homeAdapter.notifyDataSetChanged();
                refreshlayout.finishLoadmore();//传入false表示加载失败
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.saoyisao:
                break;

            case R.id.xiaoxi:
                SharedPreferences user = getActivity().getSharedPreferences("user", Context.MODE_PRIVATE);
                if (user.getString("name","")==null || user.getString("name","")== ""){
                    Toast.makeText(getActivity(),"请登录您的账号...",Toast.LENGTH_SHORT).show();
                    Intent intent = new Intent(getActivity(), LoginActivity.class);
                    startActivityForResult(intent,1);
                }else{
                    Intent intent = new Intent(getActivity(), XiaoXiActivity.class);
                    startActivity(intent);
                }
                break;
            case R.id.banner:
                break;
            case R.id.et:
                Intent intent = new Intent(getActivity(), SearchActivity.class);
                startActivity(intent);
                break;
            case R.id.vp:
                break;
            case R.id.b1:
                break;
            case R.id.b2:
                break;
            case R.id.points:
                break;
            case R.id.rv:
                break;
            case R.id.smartlayout:
                break;
            case R.id.sousuo:
                break;
        }
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {

        super.onSaveInstanceState(outState);
    }

    @Override
    public void onResume() {
        super.onResume();
        IIndexPrecenter iIndexPrecenter = new IndexPrecenter(IndexFragment.this);
        iIndexPrecenter.getDatas();
    }

    @Override
    public void onPause() {
        super.onPause();
    }
}
